//
//  PostUntils.m
//  GoldBook
//
//  Created by wmz on 2021/8/21.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "PostUntils.h"
#import <MapKit/MapKit.h>
@implementation PostUntils
+ (void)makePhoneCall:(NSString *)phoneNum {

    if (!phoneNum || phoneNum.length <= 0) {
        return;
    }
    // 号码去空格，否则生成 url 为 nil
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"<" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@">" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"(" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@")" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"+" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"-" withString:@""];
    if([phoneNum hasPrefix:@"855"]) {
        phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"855" withString:@""];
        if(![phoneNum hasPrefix:@"0"]) {
            // 按需求方要求，去掉855 补0
            phoneNum = [@"0" stringByAppendingString:phoneNum];
        }
    }
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phoneNum]];
    [[UIApplication sharedApplication] openURL:phoneURL];
}

+ (void)navigation:(NSString*)name
               lat:(double)lat
               lon:(double)lon{
    NSMutableArray *dataArr = NSMutableArray.new;
    [dataArr addObject:@"Apple Map"];
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
        [dataArr addObject:@"Google Map"];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
        [dataArr addObject:@"百度地图"];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        [dataArr addObject:@"高德地图"];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
        [dataArr addObject:@"腾讯地图"];
    }
    WMZDialog *dialog =  Dialog();
    dialog.wOKColor = wColor_yellow;
    dialog.wTitle = @"";
    dialog.wTypeSet(DialogTypeSheet).wDataSet(dataArr);
    dialog.wEventFinish = ^(id anyID, NSIndexPath *path, DialogType type) {
        if ([anyID isEqualToString:@"Apple Map"]) {
            CLLocationCoordinate2D station = CLLocationCoordinate2DMake(lat, lon);
            MKMapItem *toStation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:station addressDictionary:nil]];
            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            currentLocation.name = @"我的位置";
            toStation.name = name;
            [MKMapItem openMapsWithItems:@[currentLocation,toStation] launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        }else if ([anyID isEqualToString:@"Google Map"]) {
            NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"SuperApp",@"superApp",lat, lon];
            urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {}];
        }else if ([anyID isEqualToString:@"百度地图"]) {
            CLLocationCoordinate2D xcoordinate = [self getBaiDuCoordinateByGaoDeCoordinate:CLLocationCoordinate2DMake(lat, lon)];
            NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&mode=transit&coord_type= bd09ll",HDLocationManager.shared.coordinate2D.latitude, HDLocationManager.shared.coordinate2D.longitude,@"我的位置"];
            if (lat != 0 && lon != 0) {
                urlString = [NSString stringWithFormat:@"%@&destination=latlng:%f,%f|name:%@", urlString, xcoordinate.latitude, xcoordinate.longitude, name?:@""];
            }else{
                urlString = [NSString stringWithFormat:@"%@&destination=%@|name:%@",urlString,name?:@"",name?:@""];
            }
            urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {}];
        }else if ([anyID isEqualToString:@"高德地图"]) {
            NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=SuperApp&backScheme=superApp&slat=%f&slon=%f&sname=%@&dev=1&t=1",HDLocationManager.shared.coordinate2D.latitude, HDLocationManager.shared.coordinate2D.longitude,@"我的位置"];
            if (lat != 0 && lon != 0) {
                urlString = [NSString stringWithFormat:@"%@&dlat=%f&dlon=%f&dname=%@", urlString, lat, lon ,name?:@""];
            }else{
                urlString = [NSString stringWithFormat:@"%@&dname=%@",urlString, name];
            }
            urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {}];
        }else if ([anyID isEqualToString:@"腾讯地图"]) {
            NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=bus&fromcoord=%f,%f&from=%@&referer=superApp",HDLocationManager.shared.coordinate2D.latitude, HDLocationManager.shared.coordinate2D.longitude,@"我的位置"];
            if (lat != 0 && lon != 0) {
                urlString = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",urlString, lat, lon,name?:@""];
            }else{
                urlString = [NSString stringWithFormat:@"%@&to=%@",urlString,name?:@""];
            }
            urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {}];
        }
    };
    dialog.wEventCancelFinish = ^(id anyID, id otherData) {
      
    };
    dialog.wStart();
}


/// 转为百度坐标
+ (CLLocationCoordinate2D)getBaiDuCoordinateByGaoDeCoordinate:(CLLocationCoordinate2D)coordinate{
    return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065);
}


+ (void)shareAction:(NSString*)url text:(NSString*)text  image:(NSString*)image{
    NSString *textToShare1 = text?:@"";
    UIImage *imageToShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:image]]];
    NSURL *urlToShare = [NSURL URLWithString:url];
    NSArray *activityItems = @[textToShare1, imageToShare,urlToShare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

    //去除一些不需要的图标选项
    activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];

    //成功失败的回调block
    UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {

        if (completed){
            NSLog(@"completed");
        }else{
            NSLog(@"canceled");
        }
   };
   activityVC.completionWithItemsHandler = myBlock;
    [[BaseAlert getCurrentVC]presentViewController:activityVC animated:YES completion:nil];
}

@end
